﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static WinFormsApp1.Leo.week02.SlaughteredAdd;
using static WinFormsApp1.Leo.week02.SlaughteredUpdate;

namespace WinFormsApp1.Leo.week02
{
    public partial class SplitRequisUpdate : Form
    {
        private int splitRequisId;
        public SplitRequisUpdate(int SplitRequisId)
        {
            GetUPCData();
            InitializeComponent();
            GetProduct();
            this.splitRequisId = SplitRequisId;
        }

        //商品代号
        public async void GetUPCData()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<UPCDataser>>>(URLCommon.Url2, "/WeekListOne_Leo/GetUPCDataShow", HttpClientHelper.MethodType.GET, default);

            comboBox3.DataSource = items.Data;
            comboBox3.DisplayMember = "UPCDataName";
            comboBox3.ValueMember = "GoodsMinuteId";

            this.Controls.Add(comboBox3);
        }

        public class UPCDataser
        {
            public int GoodsMinuteId { get; set; }
            public string UPCDataName { get; set; }
        }

        //商品批号
        public async void GetProduct()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<Productser>>>(URLCommon.Url2, "/WeekListOne_Leo/GetProductShow", HttpClientHelper.MethodType.GET, default);

            comboBox2.DataSource = items.Data;
            comboBox2.DisplayMember = "ProductName";
            comboBox2.ValueMember = "ProductId";

            this.Controls.Add(comboBox3);
        }

        public class Productser
        {
            public int ProductId { get; set; }
            /// <summary>
            /// 商品批号名称
            /// </summary>
            public string ProductName { get; set; }
        }

        //反填
        private async void SplitRequisUpdate_Load(object sender, EventArgs e)
        {
            SplitRequisMang splitRequisUpdateser = new(splitRequisId);

            var items = await HttpClientHelper.RequestUrl<SplitRequisMang, ApiResult<SplitRequiser>>(URLCommon.Url2, "/WeekListOne_Leo/GetSplitRequisMang", HttpClientHelper.MethodType.POST, splitRequisUpdateser);

            this.dateTimePicker1.Text = items.Data.SplitRequisTime.ToString();
            //this.comboBox1.ValueMember=items.Data.SupplierName.ToString();
            this.comboBox2.SelectedValue = items.Data.ProductId;
            this.comboBox3.SelectedValue = items.Data.MarkId;
            this.textBox1.Text = items.Data.SplitRequisNum.ToString();
            this.textBox2.Text = items.Data.SplitRequisGrossWeight.ToString();
            this.textBox3.Text = items.Data.SplitRequisTares.ToString();
            this.textBox4.Text = items.Data.SplitRequisWeight.ToString();
            this.textBox5.Text = items.Data.SplitRequisRemark.ToString();
        }

        public record SplitRequisMang(int SplitRequisId);

        public class SplitRequiser
        {
            public int SplitRequisId { get; set; }

            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime? SplitRequisTime { get; set; }

            /// <summary>
            /// 仓库
            /// </summary>
            public int DepotId { get; set; }

            /// <summary>
            /// 商品批号
            /// </summary>
            public int ProductId { get; set; }

            public string ProductName { get; set; }

            /// <summary>
            /// 商品代号
            /// </summary>
            public int MarkId { get; set; }

            public string UPCDataName { get; set; }

            /// <summary>
            /// 件数
            /// </summary>
            public string SplitRequisNum { get; set; }

            /// <summary>
            /// 毛重
            /// </summary>
            public string SplitRequisGrossWeight { get; set; }

            /// <summary>
            /// 皮重
            /// </summary>
            public string SplitRequisTares { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string SplitRequisWeight { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string SplitRequisRemark { get; set; }

            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; } = false;
        }

        //修改
        private async void button1_Click(object sender, EventArgs e)
        {
            var SplitRequisId = splitRequisId;
            var SplitRequisTime =Convert.ToDateTime( dateTimePicker1.Text);
            var DepotId = Convert.ToInt32(comboBox1.SelectedValue);
            var ProductId = Convert.ToInt32(comboBox2.SelectedValue);
            var markId = Convert.ToInt32(comboBox3.SelectedValue);
            var SplitRequisNum = textBox1.Text;
            var SplitRequisGrossWeight = textBox2.Text;
            var SplitRequisTares = textBox3.Text;
            var SplitRequisWeight = textBox4.Text;
            var SplitRequisRemark = textBox5.Text;
            var isDelete = false;



            SplitRequists clients = new(SplitRequisId, SplitRequisTime, DepotId, ProductId, markId, SplitRequisNum, SplitRequisGrossWeight, SplitRequisTares, SplitRequisWeight, SplitRequisRemark,isDelete);
            var res = await HttpClientHelper.RequestUrl<SplitRequists, ApiResult<SplitRequiser>>(URLCommon.Url1, "/WorkList01_leo/GetSplitRequisUpdate", HttpClientHelper.MethodType.POST, clients);
            if (res.Code == 1)
            {
                MessageBox.Show("修改成功");
                this.Hide();
            }
        }

        public record SplitRequists(int SplitRequisId, DateTime SplitRequisTime, int DepotId, int ProductId, int markId, string SplitRequisNum, string SplitRequisGrossWeight
            , string SplitRequisTares, string SplitRequisWeight, string SplitRequisRemark, bool isDelete);
    }
}
